package org.noote.test_virtualpad;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.Toast;

public class TestVirtualPad_Activity extends Activity {

	static final String TAG = "TestVirtualPad_Activity";

	DisplayView _viewDisplay = null;
	
	boolean _bMultiTouch = false;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.remote);

        FrameLayout frameDisplay = (FrameLayout) findViewById(R.id.frameDisplay);
        _viewDisplay = new DisplayView(this);
		frameDisplay.addView(_viewDisplay);
		_viewDisplay.setOnTouchListener(new OnTouchListener()
		{
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				
				_viewDisplay._padInput.touch(event, _bMultiTouch);
				
				if(_viewDisplay._padScanLine.haveNewEvent())
				{
					if(_viewDisplay._padScanLine.getButton())
					{
						// do a scan line radar !
						if(!_viewDisplay._padHalf.isLocked() && !_viewDisplay._padQuick.isLocked())
							sendMessage("scanline full");
						else if(!_viewDisplay._padHalf.isLocked() && _viewDisplay._padQuick.isLocked())
							sendMessage("scanline quick");
						else if(_viewDisplay._padHalf.isLocked() && !_viewDisplay._padQuick.isLocked())
							sendMessage("scanline half");
						else if(_viewDisplay._padHalf.isLocked() && _viewDisplay._padQuick.isLocked())
							sendMessage("scanline half quick");
					}
					_viewDisplay._padScanLine.resetNewEvent();
				}
				if(_viewDisplay._padRadar.haveNewEvent())
				{
					if(_viewDisplay._padRadar.getButton())
					{
						// do a scan radar !
						if(!_viewDisplay._padHalf.isLocked() && !_viewDisplay._padQuick.isLocked())
							sendMessage("radar full");
						else if(!_viewDisplay._padHalf.isLocked() && _viewDisplay._padQuick.isLocked())
							sendMessage("radar quick");
						else if(_viewDisplay._padHalf.isLocked() && !_viewDisplay._padQuick.isLocked())
							sendMessage("radar half");
						else if(_viewDisplay._padHalf.isLocked() && _viewDisplay._padQuick.isLocked())
							sendMessage("radar half quick");
					}
					_viewDisplay._padRadar.resetNewEvent();
				}
				if(_viewDisplay._padReport.haveNewEvent())
				{
					if(_viewDisplay._padReport.getButton())
					{
						// enable/stop report transmission
						_viewDisplay._bReportTransmission = _viewDisplay._padReport.isLocked();
						sendMessage("setting:enable_report="+((_viewDisplay._bReportTransmission)?"1":"0"));
					}
					_viewDisplay._padReport.resetNewEvent();
					_viewDisplay.invalidate();
				}
				if(_viewDisplay._padBody.haveNewEvent())
				{
					if(_viewDisplay._padBody.getTouch()<0.5f || _viewDisplay._padBody.getButtonCenter())
					{
						// stop tout mouvement du vehicule !
						sendMessage("x");
					}
					else
					{
						// ondule ton corps
						if(_viewDisplay._padBody.getButtonUp())
							sendMessage("z"); // go front
						else if(_viewDisplay._padBody.getButtonDown())
							sendMessage("s"); // go back
						else if(_viewDisplay._padBody.getButtonLeft())
							sendMessage("q"); // rotate left
						else if(_viewDisplay._padBody.getButtonRight())
							sendMessage("d"); // rotate right
					}
					_viewDisplay._padBody.resetNewEvent();
					_viewDisplay.invalidate();
				}
				if(_viewDisplay._padBodySpeed.haveNewEvent())
				{
					if(_viewDisplay._padBodySpeed.getTouch()<0.5f)
					{
						// set vehicule speed !
						sendMessage("speed:"+(int)(1.0f-_viewDisplay._padBodySpeed.getPosition()*100));
					}
					_viewDisplay._padBodySpeed.resetNewEvent();
					_viewDisplay.invalidate();
				}
				if(_viewDisplay._padReinitTurrel.haveNewEvent())
				{
					if(_viewDisplay._padReinitTurrel.getButton())
					{
						// enable/stop report transmission
						sendMessage("turrel:low=0,high=0");
						_viewDisplay._padTurrel.reset();
					}
					_viewDisplay._padReinitTurrel.resetNewEvent();
					_viewDisplay.invalidate();
				}
				if(_viewDisplay._padTurrel.haveNewEvent())
				{
					if(_viewDisplay._padTurrel.getTouch()>0.5f)
					{
						// set turrel
						sendMessage("turrel:low="+_viewDisplay._padTurrel.getX()*100+",high="+_viewDisplay._padTurrel.getY()*100);
					}
					_viewDisplay._padTurrel.resetNewEvent();
					_viewDisplay.invalidate();
				}
				
				// consume motion event
		        return true;
			}
		});
    }
    
	private void sendMessage(String sMsg) {
		Toast.makeText(getApplicationContext(), sMsg, Toast.LENGTH_LONG).show();
		Log.d(TAG, sMsg);
	}
}
